﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Management;
using AvatarHelper;
using Avatar.Model;

public partial class Admin_Category : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            if (!IsPostBack)
            {
                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;

                DropDownList1.DataSource = _adc.Management_Department.Where(d => d.Open == true);
                DropDownList1.DataBind();

            }
            gv_Category.DataSource = _adc.Management_Category.Where(c => c.DepartmentID == Convert.ToInt32(DropDownList1.SelectedValue));
            gv_Category.DataBind();
        }
    }
    protected void btn_UpdateSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);

            MenuManager.CategoryUpdate(_id,
                txt_NameUpdate.Text,
                Convert.ToInt32(DropDownList1.SelectedValue),
                txt_UrlUpdate.Text,
                txt_DescriptionUpdate.Text,
                string.IsNullOrEmpty(txt_Order.Text) ? 0 : Convert.ToInt32(txt_Order.Text),
                Convert.ToBoolean(rb_Lock.SelectedValue));

            btn_Add.Visible = true;
            panel_Add.Visible = false;
            panel_Edit.Visible = false;
            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_UpdateCancel_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = true;
        panel_Add.Visible = false;
        panel_Edit.Visible = false;
        gv_Category.DataBind();
    }
    protected void btn_AddSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (MenuManager.CategoryAdd(txt_NameAdd.Text, Convert.ToInt32(DropDownList1.SelectedValue), txt_Url.Text, txt_Description.Text))
            {
                txt_NameAdd.Text = "";
                txt_Url.Text = "";
                txt_Description.Text = "";

                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;
                gv_Category.DataBind();
            }
            else
            {
                throw new Exception("添加管理单元失败：");
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "*" + _e.Message;
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        Button _b = sender as Button;
        int _id = Convert.ToInt32(_b.CommandArgument);
        var _d = MenuManager.GetCategory(_id);
        if (_d != null)
        {
            txt_NameUpdate.Text = _d.Name;
            txt_UrlUpdate.Text = _d.Url;
            txt_Order.Text = _d.Order.ToString();
            rb_Lock.SelectedValue = _d.Open.ToString();
            txt_DescriptionUpdate.Text = _d.Description;
            btn_UpdateSubmit.CommandArgument = _d.ID.ToString();

            btn_Add.Visible = false;
            panel_Add.Visible = false;
            panel_Edit.Visible = true;
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
        try
        {
            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);

            MenuManager.CategoryDelete(_id);

            btn_Add.Visible = true;
            panel_Add.Visible = false;
            panel_Edit.Visible = false;
            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "操作失败" + _e.Message;
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = false;
        panel_Add.Visible = true;
        panel_Edit.Visible = false;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        btn_Add.Visible = true;
        panel_Add.Visible = false;
        panel_Edit.Visible = false;
        //using (var _adc = DBHelper.GetDataContext())
        //{
        //    gv_Category.DataSource = _adc.Management_Category.Where(c => c.DepartmentID == Convert.ToInt32(DropDownList1.SelectedValue));
        //    gv_Category.DataBind();
        //}
    }
}
